package cn.gov.lyhdga.first.web.message;

import java.util.Map;

import javax.servlet.ServletRequest;
import javax.validation.Valid;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springside.modules.web.Servlets;

import cn.gov.lyhdga.first.entity.Message;
import cn.gov.lyhdga.first.entity.User;
import cn.gov.lyhdga.first.service.account.ShiroDbRealm.ShiroUser;
import cn.gov.lyhdga.first.service.message.MessageService;

import com.google.common.collect.Maps;

@Controller
@RequestMapping(value = "/message")
public class MessgeController {
	
	private static final String PAGE_SIZE = "5";
	
	private static Map<String, String> sortTypes = Maps.newHashMap();
	
	static {
		sortTypes.put("auto","自动");
		sortTypes.put("title","标题");
	}

	@Autowired
	private MessageService messageService;
	
	@RequestMapping(method = RequestMethod.GET)
	public String list(@RequestParam(value = "page", defaultValue = "1") int pageNumber,
			@RequestParam(value = "page.size", defaultValue = PAGE_SIZE) int pageSize,
			@RequestParam(value = "sortType", defaultValue = "auto") String sortType, Model model,
			ServletRequest request){
		Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");
		Long userId = getCurrentUserId();
		
		Page<Message> messages = messageService.getUserMessage(userId, searchParams, pageNumber, pageSize, sortType);
		
		model.addAttribute("messages", messages);
		model.addAttribute("sortType", sortType);
		model.addAttribute("sortTypes", sortTypes);
		
		// 将搜索条件编码成字符串，用于排序，分页的URL
		model.addAttribute("searchParams", Servlets.encodeParameterStringWithPrefix(searchParams, "search_"));
		
		return "message/messageList";
	}
	
	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(Model model){
		model.addAttribute("message", new Message());
		model.addAttribute("action", "create");
		
		return "message/messageForm";
	}
	
	@RequestMapping(value = "create", method = RequestMethod.POST)
	public String create(@Valid Message newMessage, RedirectAttributes redirectAttributes){
		User user = new User(getCurrentUserId());
		newMessage.setUser(user);
		
		messageService.saveMessage(newMessage);
		redirectAttributes.addFlashAttribute("message","创建信息成功");
		
		return "redirect:/message/";
	}
	
	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("id") Long id, Model model){
		model.addAttribute("message", messageService.getMessage(id));
		model.addAttribute("action", "update");
		
		return "message/messageForm";
	}
	
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(@Valid @ModelAttribute("message") Message message, RedirectAttributes redirectAttributes){
		messageService.saveMessage(message);
		redirectAttributes.addFlashAttribute("message", "更新信息成功");
		
		return "redirect:/message/";
	}
	
	@RequestMapping(value = "delete/{id}")
	public String delete(@PathVariable("id") Long id, RedirectAttributes redirectAttributes){
		messageService.deleteMessage(id);
		redirectAttributes.addFlashAttribute("message", "删除信息成功");
		
		return "redirect:/message/";
	}
	
	@ModelAttribute
	public void getMessage(@RequestParam(value = "id", defaultValue = "-1") Long id, Model model){
		if(id != -1){
			model.addAttribute("message", messageService.getMessage(id));
		}
	}
	
	/**
	 * 取出Shiro中的当前用户Id.
	 */
	private Long getCurrentUserId() {
		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		return user.id;
	}
}
